package com.gali.leetcode.day23;

import com.gali.leetcode.day4.Roman;

/**
 * @Auther: gali
 * @Date: 2022-08-12 19:04
 * @Description:
 */
public class Solution {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 验证二叉搜索树---递归
     * @param root
     * @return
     */
    public boolean isValidBST(TreeNode root) {
        return isValid(root,Long.MIN_VALUE,Long.MAX_VALUE);
    }

    private boolean isValid(TreeNode root, long minValue, long maxValue) {
        if (root==null){
            return true;
        }
        if (root.val<=minValue||root.val>=maxValue){
            return false;
        }
        return isValid(root.left,minValue,root.val)&&isValid(root.right,root.val,maxValue);
    }

    /**
     * 对称二叉树
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        return check(root,root);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if (left==null && right==null){
            return true;
        }
        if (left==null||right==null){
            return false;
        }
        return left.val==right.val&&check(left.left,right.right)&&check(left.right,right.left);
    }
}
